package com.ponhu.ea2.goodsCenter.provider.schedule;

import com.alibaba.fastjson.JSON;
import com.ponhu.ea2.entity.GoodsDataSyncTaskLog;
import com.ponhu.ea2.enums.SyncTaskTypeEnum;
import com.ponhu.ea2.goodsCenter.provider.service.GoodsDataSyncService;
import com.ponhu.ea2.goodsCenter.provider.service.GoodsDataSyncTaskLogService;
import com.ponhu.ea2.goodsCenter.provider.service.PonHuGoodsDataService;
import com.ponhu.ea2.query.GoodsDataSyncQuery;
import com.ponhu.ea2.vo.GoodsDataStockSyncVo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Component
public class StockUpdateDataSyncTask {
    @Resource
    PonHuGoodsDataService ponHuGoodsDataService;
    @Resource
    GoodsDataSyncService goodsStockDataSync;
    @Resource
    GoodsDataSyncTaskLogService goodsDataSyncTaskLogService;

    /**
     * 同步商品货品库存数，方案已取消，此功能废弃
     * @param param
     * @return
     */
    @Deprecated
    @XxlJob("stockUpdateDataSyncTask")
    public ReturnT<String> stockUpdateDataSyncTask(String param) {
        log.info("商品数据同步库存数开始---------Start！");
        List<GoodsDataStockSyncVo> goodsDataStockSyncVoList = new ArrayList<>();
        // 取出最后一次执行任务的最新时间
        Date lastDateEditTime = goodsDataSyncTaskLogService.getLastDataEditTime(SyncTaskTypeEnum.STOCK.getCode());
        // 查询条件对象
        GoodsDataSyncQuery goodsDataSyncQuery = new GoodsDataSyncQuery();

        // 保存任务执行日志
        GoodsDataSyncTaskLog goodsDataSyncTaskLog = new GoodsDataSyncTaskLog();
        goodsDataSyncTaskLog.setStartTime(LocalDateTime.now());

        if (null != lastDateEditTime) {
            goodsDataSyncQuery.setDataEditTime(lastDateEditTime);
            goodsDataStockSyncVoList = ponHuGoodsDataService.selectPhGoodsStockData(goodsDataSyncQuery);
        } else {
            goodsDataStockSyncVoList = ponHuGoodsDataService.selectPhGoodsStockData(goodsDataSyncQuery);
        }
        // 判断list是否为空
        if (!CollectionUtils.isEmpty(goodsDataStockSyncVoList)) {
            // 保存日志的最新更新时间和货品编码值
            goodsDataSyncTaskLog.setDataEditTime(goodsDataStockSyncVoList.get(0).getDataEditTime());
            goodsDataSyncTaskLog.setCode(goodsDataStockSyncVoList.get(0).getCode());
            //更新库存数据
//            goodsStockDataSync.updateGoodsStockData(goodsDataStockSyncVoList);
        }
        // 任务结束时间
        goodsDataSyncTaskLog.setEndTime(LocalDateTime.now());
        Map<String, Object> remarksMap = new HashMap<>();
        remarksMap.put("取出胖总管条数", goodsDataStockSyncVoList.size());
        goodsDataSyncTaskLog.setRemarks(JSON.toJSONString(remarksMap));
        goodsDataSyncTaskLog.setTaskType(SyncTaskTypeEnum.STOCK.getCode());
        goodsDataSyncTaskLogService.save(goodsDataSyncTaskLog);
        return ReturnT.SUCCESS;
    }
}
